1 Welcome

Using Git and GitHub is a great way to code collaborativly on projects. Please use this guide to get set up or as reference for git commands.

2 Setup

2.1 Install Git Bash

Go to git-scm.com/download and click on the “Windows” link under "Downloads on the main screen.

Update Git Bash

git update-git-for-windows

2.2 Set Up GitHub Account

Go to GitHub

Go to github.com and click either on the large green button “Sign Up for GitHub” in the middle of the page or the small button “Sign Up” in the top right corner of the page.

Create Your Account

  1. Create your own personal username
  2. Type in your email (SPRA email)
  3. Create your personal password
  4. Click on the large blue button “Create account”
  • Optional: un-check box so you don’t recieve additional emails from GitHub
  • Note: You may have to ‘verify’ account by completing some Captcha.

2.3 Access Repositories

  1. Inform relavent colleague(s) of the username you created (it’s usually Josh)
  2. Once username is given, individuals can give you an invitation to access to a repository, which you will receive via email
  3. Accept the invitation by clicking “View Invitation” button sent in the email. This will take you to the GitHub page and now you can access the repository.

3 Basic Git Processes

3.1 Create Clone

  1. Go to repository webpage
  2. Click on the green “Code” button and copy the HTTPS path using the clipboard button
  3. Go to the place on your machine where you want to have the code
  4. Note: Interacting with GitHub will be much faster if you put the code directly on your machine rather than on your personal hal folder. I like to keep all my clones C:\PROJECT-CLONES. This also makes the pathways shorter because it doesn’t inlcude your name.
  5. Right click in the folder and select “Git Bash Here”
  6. Git Bash window will appear with the path reflecting the path
  7. Write the command git clone <repo HTTPS path> where <repo HTTPS path> is the url from step 1; press Enter
  8. Note: You can’t copy/paste using Ctr+C/Ctr+V, but you can copy/paste using Ctr+C/Shift+Insert
  9. A folder will appear which has the same name as the repository, all files from the repo will be download there (in this example it’s just a README.md file)

3.2 Make Edits to Code

  1. Go into repository folder on machine, right click and select “Git Bash Here”
  2. PRIOR TO MAKING ANY EDITS PULL MOST RECENT CODE FROM REPO: git pull origin <branch>
  3. Most often the <branch> you will be working on is main or master. The branch you are working on will show up in blue font in parenthesis in the git bash window. Pulling the most recent code is essential for collaborative coding, this helps prevents conflicts to the code if edits are made on multiple different versions.
    Example: Code on Machine Already Up-to-Date (no changes were made)
    Example: Code on Machine Updated (changes to code on machine)
  4. After you have pulled any changes, now you can start editing your document
  5. After edits are complete, repeat step 1 to open the git bash window
  6. See which files were added, edited, or deleted using git status
  7. In this case, I have made edits to the README.md file.
  8. Review the changes by using git diff
  9. git diff will show all changes for any file, if you only want to look at changes for a specific file use git diff <file>.
    Red font means that text has been deleted and green font means that text has been added.
  10. Once you are comfortable with your changes you need to stage the files, or ‘add’ them so they can be committed.
    • git add --all will stage all files that are listed after the git status command
    • git add <file> will add only a specific file
    • git add *.R will add all files that have a .R extension (stage all R scripts)
  11. Check that your files have been staged by using the git status command again, files that were in red font should now be in green font, indicating that they have been staged and are ready to be committed.
  12. Commit changes using git commit -m '<message>' where <message> is a short description of the edits.
  13. Finally, push your commits to GitHub using git push origin <branch> Most often the <branch> you will be working on is main or master.

3.2.1 Simple Review

  • PULL: git pull origin <branch>; to update code
  • EDIT: Make edits to files
  • Optional: git status to view which files have changed
  • Optional: git diff to view line by line changes
  • STAGE: git add --all to stage all files
  • COMMIT:git commit -m '<message>' to commit changes
  • PUSH: git push origin <branch> to send changes to GitHub so others can access

3.2.2 Flow Charts







4 Common Commands

4.1 Git Commands

  • Generally you will be working on the main or master branch
  • <file> must include file name and the extension (i.e. ‘test.sas’ or ‘test.R’ not just ‘test’)
command description
git add --all stage ALL files that have been changed/added/deleted
git add <file> Stage a specific <file>
git checkout -b <branch> Create a new <branch> and switch to that <branch>
git checkout <branch> Switch to a specific <branch>
git checkout <file> Revert <file> to most recent commit on local repo
git commit -m '<message>' Commit changes with short <message>
git diff --stat --cached origin/<branch> List of files that have been commited (list of files that will be included in next git push)
git diff <file> View changes for a specific <file>; if you just use git diff all changes for all files will be shown. If you have a lot of changes it will end with an (END) on the screen, to quit this screen press the Q key
git fetch origin <branch> See if there are any changes/updates from a specific <branch>; could do this prior to git pull
git log -<numb> View a list of the most recent <numb> commits; e.g. git log -2 will show 2 commits
git pull origin <branch> Pull from specific <branch>; will generally be main or master
git push origin <branch> Push to specific <branch>; will generally be main or master
git reset Undo git add; will un-stage all files; can also use git reset <file> to unstage specific file
git status see which files have been changed/added/deleted on working branch

What if I want to ignore any differences on my clone and ‘reset’ to the version on GitHub?

  1. git fetch origin <branch>
    This will get the most recent versions from GitHub
  2. git reset --hard origin/<branch>
    This will change all files in your clone repository to match the most up-to-date version from GitHub

This should be done rarely because any changes you have worked on will not be saved.

4.2 UNIX Commands

In addition to git command, you can use UNIX commands in the bash window.

command description
cd .. Move up one folder; e.g. go from C:/Documents/<folder> to C:/Documents
cd <folder> Move down into a specific folder; e.g. go from C:/Documents to C:/Documents/<folder>
ls -l View files in current folder and their recent mondification